//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "TqrEigenDecomposition.h"
using namespace Cephei::QL::Math::Matrixutilities;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Math/Matrix.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::CTqrEigenDecomposition (Cephei::QL::Math::IArray^ diag, Cephei::QL::Math::IArray^ sub, Microsoft::FSharp::Core::FSharpOption<QL::Math::Matrixutilities::TqrEigenDecomposition::EigenVectorCalculationEnum>^ calc, Microsoft::FSharp::Core::FSharpOption<QL::Math::Matrixutilities::TqrEigenDecomposition::ShiftStrategyEnum>^ strategy) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CArray^ _Cdiag;
    CArray^ _Csub;
    try
    {
#ifdef HANDLE
        _phTqrEigenDecomposition = NULL;
#endif
        _Cdiag = safe_cast<CArray^> (diag);
        _Cdiag->Lock();
        QuantLib::Array& _diag = static_cast<QuantLib::Array&> (_Cdiag->GetReference ()); 
        _Csub = safe_cast<CArray^> (sub);
        _Csub->Lock();
        QuantLib::Array& _sub = static_cast<QuantLib::Array&> (_Csub->GetReference ()); 
        QuantLib::TqrEigenDecomposition::EigenVectorCalculation _calc = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Math::Matrixutilities::TqrEigenDecomposition::EigenVectorCalculationEnum>::IsSome::get (calc) ? (QuantLib::TqrEigenDecomposition::EigenVectorCalculation)calc->Value : QuantLib::TqrEigenDecomposition::EigenVectorCalculation::WithEigenVector); //10
        QuantLib::TqrEigenDecomposition::ShiftStrategy _strategy = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Math::Matrixutilities::TqrEigenDecomposition::ShiftStrategyEnum>::IsSome::get (strategy) ? (QuantLib::TqrEigenDecomposition::ShiftStrategy)strategy->Value : QuantLib::TqrEigenDecomposition::ShiftStrategy::CloseEigenValue); //10
        _ppTqrEigenDecomposition = new boost::shared_ptr<QuantLib::TqrEigenDecomposition> (new QuantLib::TqrEigenDecomposition ( _diag,  _sub,  _calc,  _strategy ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdiag != nullptr) _Cdiag->Unlock();
        if (_Csub != nullptr) _Csub->Unlock();
    }
}
Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::CTqrEigenDecomposition (boost::shared_ptr<QuantLib::TqrEigenDecomposition>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTqrEigenDecomposition = NULL;
#endif
	_ppTqrEigenDecomposition = &childNative;
    
}
Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::CTqrEigenDecomposition (QuantLib::TqrEigenDecomposition& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phTqrEigenDecomposition = NULL;
#endif
	_ppTqrEigenDecomposition = new boost::shared_ptr<QuantLib::TqrEigenDecomposition> (&childNative);
    
    _TqrEigenDecompositionOwner = owner;
    
}

Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::CTqrEigenDecomposition (CTqrEigenDecomposition^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTqrEigenDecomposition = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppTqrEigenDecomposition = new boost::shared_ptr<QuantLib::TqrEigenDecomposition> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::CTqrEigenDecomposition (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTqrEigenDecomposition = NULL;
#endif
	if (!t->IsSubclassOf(CTqrEigenDecomposition::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::CTqrEigenDecomposition (QuantLib::Handle<QuantLib::TqrEigenDecomposition>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phTqrEigenDecomposition = &childNative;
	_ppTqrEigenDecomposition = &static_cast<boost::shared_ptr<QuantLib::TqrEigenDecomposition>>(childNative.currentLink());
    
    _TqrEigenDecompositionOwner = owner;
}
Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::CTqrEigenDecomposition (QuantLib::Handle<QuantLib::TqrEigenDecomposition> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phTqrEigenDecomposition = &childNative;
	_ppTqrEigenDecomposition = &static_cast<boost::shared_ptr<QuantLib::TqrEigenDecomposition>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::CTqrEigenDecomposition (QuantLib::TqrEigenDecomposition childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTqrEigenDecomposition = NULL;
#endif
	_ppTqrEigenDecomposition = new boost::shared_ptr<QuantLib::TqrEigenDecomposition> (new QuantLib::TqrEigenDecomposition (childNative));
    
}
#endif

Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::~CTqrEigenDecomposition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppTqrEigenDecomposition != NULL)
    {
	    delete _ppTqrEigenDecomposition;
        _ppTqrEigenDecomposition = NULL;
    }
}
Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::!CTqrEigenDecomposition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppTqrEigenDecomposition != NULL)
    {
	    delete _ppTqrEigenDecomposition;
    }
}
QuantLib::TqrEigenDecomposition& Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::GetReference ()
{
    if (_ppTqrEigenDecomposition == NULL) throw gcnew NativeNullException ();
	return **_ppTqrEigenDecomposition;
}
boost::shared_ptr<QuantLib::TqrEigenDecomposition>& Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::GetShared ()
{
    if (_ppTqrEigenDecomposition == NULL) throw gcnew NativeNullException ();
	return *_ppTqrEigenDecomposition;
}
QuantLib::TqrEigenDecomposition* Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::GetPointer ()
{
    if (_ppTqrEigenDecomposition == NULL) throw gcnew NativeNullException ();
	return &**_ppTqrEigenDecomposition;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::TqrEigenDecomposition>& Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::GetHandle ()
{
	if (_phTqrEigenDecomposition == NULL)
	{
		_phTqrEigenDecomposition = new Handle<QuantLib::TqrEigenDecomposition> (*_ppTqrEigenDecomposition);
	}
	return *_phTqrEigenDecomposition;
}
#endif
bool Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::HasNative () 
{
	return (_ppTqrEigenDecomposition != NULL);
}

Cephei::QL::Math::IArray^ Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::Eigenvalues::get ()
{
    try
    {
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppTqrEigenDecomposition)->eigenvalues ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::Eigenvectors::get ()
{
    try
    {
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppTqrEigenDecomposition)->eigenvectors ( );   
        Cephei::QL::Math::CMatrix^ _nrv = gcnew Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition::Iterations::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppTqrEigenDecomposition)->iterations ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Matrixutilities::ITqrEigenDecomposition^ Cephei::QL::Math::Matrixutilities::CTqrEigenDecomposition_Factory::Create (Cephei::QL::Math::IArray^ diag, Cephei::QL::Math::IArray^ sub, Microsoft::FSharp::Core::FSharpOption<QL::Math::Matrixutilities::TqrEigenDecomposition::EigenVectorCalculationEnum>^ calc, Microsoft::FSharp::Core::FSharpOption<QL::Math::Matrixutilities::TqrEigenDecomposition::ShiftStrategyEnum>^ strategy)
{
    return gcnew CTqrEigenDecomposition ( diag,  sub,  calc,  strategy);
}
